/* 
 * File:   BVHNode.h
 * Author: brady
 *
 * Created on August 27, 2011, 5:14 PM
 */

#ifndef BVHNODE_H
#define	BVHNODE_H
#include <vector>
#include <acceleration/IntersectionStructure.h>
#include <shapes/Shape.h>

using namespace std;

class BVHNode : public Shape{
public:
    const static int X_AXIS = 0;
    const static int Y_AXIS = 1;
    const static int Z_AXIS = 2;
    BVHNode();
    BVHNode(vector<Shape*>& shapes, int sortAxis);
    BVHNode(const BVHNode& orig);
    virtual ~BVHNode();
    Shape* getShape(){return shape;}
    bool isLeaf(){return leftChild == NULL && rightChild == NULL;}
    bool testIntersect(IntersectionParams& ip, IntersectionResult& ir);
    void boundingBoxIntersections(const BoundingBox& box, vector<Shape*>& list);

private:
    void buildBoundingBox();
    Shape* shape;
    BVHNode* leftChild;
    BVHNode* rightChild;

//private comparitor class
    class ShapeComparitor{
    public:
        ShapeComparitor(int sortAxis)
            {
                this->sortAxis = sortAxis;
            }

            bool operator() (Shape* s1, Shape* s2)
            {
                double s1Val = s1->getBoundingBox().getCenter()[sortAxis];
                double s2Val = s2->getBoundingBox().getCenter()[sortAxis];
               return (s1Val < s2Val);
            }

            int sortAxis;
        };

};

#endif	/* BVHNODE_H */

